<?php

namespace app\controller\wmapi\examineapprove;

use app\controller\wmapi\IndexController;
use app\controller\wmapi\PublicCommentController;
use app\model\WaChannel;
use app\model\WaMonthlySettlement;
use app\model\WaOrdersPurchase;
use app\model\WaOrdersPurchaseContract;
use app\model\WaPayrollRequisitionExamineApproveFlow;
use app\model\WaPayrollRequisitionExamineApprove;
use app\model\WaPurchasedVehiclesAmountDetail;
use app\model\WaPurchasePayrollRequisition;
use Illuminate\Support\Facades;
use Illuminate\Support\Facades\Validator;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Admin;
use support\Request;
use app\model\WaAdmins;
use app\model\WaRoles;
use app\model\WaMembers;
use app\model\WaRules;
use app\model\WaDepartment;
use app\model\WaAdminRoles;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Cache;
use Tinywan\Validate\Validate;

class PayrollRequisitionApproveController extends Validate
{
    /**
     * 请款审批 -列表-分页
     * @param Request $request
     * @return mixed
     */
    public function payrollRequisitionApproveList(Request $request)
    {
        $param = $request->all();
        // 获取管理员信息
        $adminId = json_decode(adminId($request));
        // 获取的是 请款审批id  因为记录不删 合同编号会存在重复
        $purchasecontractsn = WaPayrollRequisitionExamineApproveFlow::allIn($adminId);
        $purchasecontractsn = json_decode($purchasecontractsn);
        $fields = ['*'];
        $condition = [];
        $between = [];
        $whereNotIn = [5,6];
//        if ($param['id']) {
//            $condition [] = ['id', '=',$param['id']]; // 采购请款审批编号
//        }

        if ($param['initiator_admin_name']) {
            $condition [] = ['initiator_admin_name', 'like', '%' . $param['initiator_admin_name'] . '%'];
        }

        if ($param['purchase_payroll_sn']) {
            $condition [] = ['purchase_payroll_sn', 'like', '%' . $param['purchase_payroll_sn'] . '%']; // 采购请款编号
        }

        $page = page($param['page']);
        $pageNum = pageNum($param['pageNum']);

        if ($param['start_time'] && $param['end_time']) {
            $between [] = [$param['start_time'], $param['end_time']];
        }
        $rule = WaPayrollRequisitionExamineApprove::list($adminId,$page, $pageNum,$whereNotIn, $purchasecontractsn,$fields, $condition, $between);

        return IndexController::successed($rule, '请款列表');
    }


    /**
     * 请款待审批 - 审批人 - 列表
     * @param Request $request
     * @return mixed
     */
    public function payrollRequisitionWaitApproveByAdminIdList(Request $request)
    {
        $param = $request->all();
        // 获取管理员信息
        $adminId = json_decode(adminId($request));
        // 获取的是 请款审批id  因为记录不删 合同编号会存在重复
        $purchasecontractsn = WaPayrollRequisitionExamineApproveFlow::allIn($adminId);
        $purchasecontractsn = json_decode($purchasecontractsn);
        $fields = ['*'];
        $condition = [];
        $between = [];


        $page = page($param['page']);
        $pageNum = pageNum($param['pageNum']);


        $rule = WaPayrollRequisitionExamineApprove::waitApproveByAdminIdList($param, $adminId,$page, $pageNum,$purchasecontractsn,$fields, $condition, $between);

        return IndexController::successed($rule, '请款列表');
    }

    /**
     * 请款审批-创建
     * @param Request $request
     * @return \support\Response
     */
    public function payrollRequisitionApproveCreate(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));

        $data = [
            'purchase_payroll_sn' => $param['purchase_payroll_sn'], //请款编号
            'admin_id' => $adminId->id, //管理员id
            'admin_name' => $adminId->nickname, //管理员名称
//            'contract_name' => $param['contract_name'], //合同名称
            'organization_code' => $param['organization_code'], //组织机构信用证代码
            'linkman' => $param['linkman'], //联系人名称
            'linkman_phone' => $param['linkman_phone'], //联系人电话
            'linkman_email' => $param['linkman_email'], //联系人邮箱
            'linkman_address' => $param['linkman_address'], //联系人地址
            'supplier_id' => $param['supplier_id'], //供应商id
            'supplier_name' => $param['supplier_name'], //供应商名字
            'supplier_type' => $param['supplier_type'], // 供应商类型： 1车辆供应商 2上牌供应商 3 国际货运代理 4保险供应商 5 国内物流
            'linkman_province' => $param['linkman_province'], //联系人省
            'linkman_city' => $param['linkman_city'], //联系人市
            'linkman_area' => $param['linkman_area'], //联系人区
            'bank_account_name' => $param['bank_account_name'], //银行户名
            'bank_open_account_name' => $param['bank_open_account_name'], //开户行名称
            'bank_account' => $param['bank_account'], //银行账号
            'delivery_province' => $param['delivery_province'], //交付省
            'delivery_city' => $param['delivery_city'], //交付市
            'delivery_area' => $param['delivery_area'], //交付区
//            'delivery_address' => $param['delivery_address'], //交付地址
            'latest_delivery_time' => $param['latest_delivery_time'], //最晚交付时间
            'guide_price' => $param['guide_price'], //指导价
            'retail_concessions' => $param['retail_concessions'], //优惠金额（产品零售优惠）
//            'other_price' => $param['other_price'], //其它费用
            'new_car_fare' => $param['new_car_fare'], //新车开票价
            'contract_price' => $param['contract_price'], //合同金额
            'earnest_price' => $param['earnest_price'], //定金金额
//            'delivery_accessories' => $param['delivery_accessories'], //交车附件
            'supplementary_terms' => $param['supplementary_terms'], //补充条款
            'created_at' => dateTime(),
            'status' => 0, // 0 草稿 1审批中 2待供应商签署 3已完成 4已作废 5 审批不通过
        ];

        $where = ['purchase_payroll_sn' => $param['purchase_payroll_sn']];
        $find = WaPayrollRequisitionExamineApprove::find($adminId,$where);
        $find = json_decode($find);
        if ($find){
            if (in_array($find->status,[0,1,2,3])){
                return IndexController::errored('','该合同的请款已存在');
            };
        }
        \support\Db::beginTransaction();
        try {
            $res = WaPayrollRequisitionExamineApprove::create($data);
            \support\Db::commit();
            return IndexController::successed($res, '采购请款创建成功');
        } catch (\Throwable $exception) {
            IndexController::logError('payrollRequisitionApproveCreate', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '采购请款创建失败');
        }
    }

    /**
     * 请款审批-状态更新
     * @param Request $request
     * @return \support\Response
     */
    public function payrollRequisitionApproveUpdate(Request $request)
    {
//        process($request);
        $param = $request->all();
        $condition = [];
//        $condition []= ['pr_no', '=', $param['pr_no']];
        $adminId = json_decode(adminId($request));
        if (array_key_exists('refuse_reason',$param)){
            $refuse_reason = $param['refuse_reason'];
        }else{
            $refuse_reason = '';
        }
        // 根据请款单编号获取请款单信息
        $payroll_orders = WaPurchasePayrollRequisition::find(['purchase_payroll_sn' => $param['purchase_payroll_sn']]);
        $payroll_orders = json_decode($payroll_orders);
        if (!$payroll_orders) {
            return IndexController::errored('', '获取请款单信息失败');
        }

        // 当前请款审批合同数据
//        $approves = WaPayrollRequisitionExamineApprove::find($adminId,['id' => $param['id']]);
        $approves = WaPayrollRequisitionExamineApprove::find($adminId,['purchase_payroll_sn' => $param['purchase_payroll_sn']]);
        $approves = json_decode($approves);
        if (!$approves){
            return IndexController::errored('','找不到该请款');
        }
        $param['id'] = $approves->id;
        $flow= [];// 当前部门审批流
        $flow_next = []; // 下级部门审批流
        // 当前合同审批流
        $flows = WaPayrollRequisitionExamineApprove::find($adminId,['purchase_payroll_sn' => $approves->purchase_payroll_sn]);
        $flows = json_decode($flows);
        if (!$flows){
            return IndexController::errored('', '未获取请款的审批流');
        }
        // 获取合同中的审批流设置
        $flows->approve_flow = json_decode($flows->approve_flow);


        $condition_flow = [];
        $condition_flow [] = ['pr_no', '=', $approves->pr_no];
        $condition_flow [] = ['status', '=', 0];
        $approve_flow = WaPayrollRequisitionExamineApproveFlow::find($condition_flow);
        $approve_flow = json_decode($approve_flow);
        if (!$approve_flow) {
            return IndexController::errored('', '找不到该审批的待审批记录');
        }else{
            if (!in_array($adminId->id, json_decode($approve_flow->admin_ids))){
                return IndexController::errored('', '当前审批你没有审批权限');
            }

            foreach ($flows->approve_flow as $k){
                //获取合同中该用户部门的 审批流
                if ($approve_flow->admin_ids == json_encode($k->approve_admin_id)) {
                    $flow = $k;
                }
            }
            if(!$flow){
                return IndexController::errored('ErrorApproveSetting','当前用户所在部门未设置审批流程，无法提交审批');
            }


        }


        PublicCommentController::请款审批状态;
        \support\Db::beginTransaction();
        try {
            if ($param['status'] == 0){
                return IndexController::errored('','审批只有通过和驳回和撤回状态哦');
            }elseif ($param['status'] == 1){
                // 审批通过 判断审批流是否走到最后一步 -> 如果是则多一步创建请款   =》 都需要 添加请款审批详情记录
                if ($approves){
                    // 首先这请款单必须是审批中的状态
                    if($approves->status != 0){
                        return IndexController::errored('','请款单不是审批状态无法审批');
                    }
                    // 需要判断主订单是否是审批中的状态
                    if ($approves->approve_flow){
//                    foreach (json_decode($approves->approve_flow) as $k){
                        // 然后判断 当前部门是否审批过 循环所有 审批流的所有部门 正常是按正序循环 找出上级部门为 0 的部门
                        //先查找是否有审批记录 按流程每一个审批操作会自动创建下一个 一条审批或待审批数据
                        $data = [];
                        $data [] = ['check_id', '=', $param['id']];
                        $data [] = ['status', '=', 0];
                        $check_parent = WaPayrollRequisitionExamineApproveFlow::find($data);
                        $check_parent =json_encode($check_parent);
                        // 只有查到待审批数据的部门才有审批权限
                        if ($check_parent){
                            $approve_log_condition = [];
                            $approve_log_condition [] = ['pr_no', '=', $approves->pr_no];
                            $approve_log_condition [] = ['status', '=', 0];
                            $approve_log_condition [] = ['admin_ids', '=', $approve_flow->admin_ids];
                            // 添加请款审批详情记录
                            $approve_log_data = [
                                'updated_at' => dateTime(),
                                'admin_id' => $adminId->id,
                                'admin_name' => $adminId->nickname,
                                'refuse_reason' => $refuse_reason,
                                'status' => 1, //0 待审批 1审批通过 2 审批驳回
                            ];
                            if ($approves->purchase_payroll_sn) {
                                $approve_log_data['purchase_payroll_sn'] = $approves->purchase_payroll_sn;// 请款编号
                            }
                            $up_purchase_flow_status = WaPayrollRequisitionExamineApproveFlow::updated($approve_log_condition,$approve_log_data); // 添加请款审批详情记录
                            if (!$up_purchase_flow_status){
                                return IndexController::errored('','采购请款单审批流日志创建失败');
                            }
                            // 判断审批流到哪一步 如果结束则 修改合同状态 创建合同
                            //判断是否是最后一级审批
                            if ($flow->next_sort == 0){
                                // 最后一级审批  则修改审批状态
                                if ($approves->purchase_payroll_sn){
                                    $find_purchase_payroll_status =  WaPurchasePayrollRequisition::find(['purchase_payroll_sn' => $approves->purchase_payroll_sn ]);
                                    $find_purchase_payroll_status = json_decode($find_purchase_payroll_status);
                                    if (!$find_purchase_payroll_status){
                                        return IndexController::errored('','采购请款信息查询失败');
                                    }
                                    // 采购请款单状态 0 草稿(撤回审批)  1 审批中(提交审批) 2 待付款 3 已付款 4 审核不通过 5 删除
                                    // 修改已请款金额
                                    $requested_price = round($find_purchase_payroll_status->requested_price, 2) + round($find_purchase_payroll_status->this_request_price, 2);
                                    $up_purchase_payroll_status = WaPurchasePayrollRequisition::updated(['status' => 2,'requested_price' => round($requested_price,2), 'updated_at' =>dateTime()],['purchase_payroll_sn' => $approves->purchase_payroll_sn ]);// 修采购请款单状态
                                    if (!$up_purchase_payroll_status){
                                        return IndexController::errored('','采购请款单状态修改失败');
                                    }
                                    // 手动创建的请款
                                    $up_purchase_approve_status =WaPayrollRequisitionExamineApprove::updated(['id' => $param['id'] ],['status' => 1, 'updated_at' =>dateTime()]); // 修改请款审批单状态
                                    if (!$up_purchase_approve_status){

                                        return IndexController::errored('','采购请款单审批状态修改失败');
                                    }
                                    PublicCommentController::请款单请款结算类型;
                                    if (!in_array($payroll_orders->price_name,['采购定金','采购尾款'])){
                                        // 请款类型 1现结（车务）  3月结
                                        if ($payroll_orders->payroll_requisition_type == 3) {
                                            $monthly_settlement_condition = [];
                                            $monthly_settlement_condition [] = ['bill_number','=',$payroll_orders->bill_number];
                                            $monthly_settlement_data=[
                                                'settlement_sratus' => 2,// （作废 在列表中自定义生成）结算状态 1 未结清 2 结款中(请款创建、作废需要修改) 3 已结清(财务付款需要修改) 4 撤回(即删除 订单中的状态需求变回待请款)
                                                'updated_at' => dateTime(),
                                            ];
                                            $monthly_settlement_up = WaMonthlySettlement::updated($monthly_settlement_data,$monthly_settlement_condition);
                                            if (!$monthly_settlement_up) {
                                                return IndexController::errored('', '月结结算状态同步失败');
                                            }

                                        }elseif ($payroll_orders->payroll_requisition_type == 1){
                                            $cash_settlement_condition = [];
                                            $cash_settlement_condition [] = ['vin','=',$payroll_orders->vin];
                                            $cash_settlement_condition [] = ['price_name','=',$payroll_orders->price_name];
                                            $cash_settlement_data=[
                                                'payment_status' => 2,// 付款类型  0 待请款 1审批中 2 未付款 3 已付款 4 审核不通过 5 撤回 6 删除
                                                'updated_at' => dateTime(),
                                            ];
                                            $cash_settlement_up = WaPurchasedVehiclesAmountDetail::updated($cash_settlement_data,$cash_settlement_condition);
                                            if (!$cash_settlement_up) {
                                                return IndexController::errored($payroll_orders->price_name, '现结结算状态同步失败');
                                            }
                                        }
                                    }


                                }

                            }else{
                                $next_department = $flow->next_sort; //下一级部门
                                foreach ($flows->approve_flow as $k){
                                    //获取合同中该用户部门的 审批流
                                    if ($k->sort == $next_department) {
                                        $flow_next = $k;
                                    }

                                }
                                if($flow_next){
                                    // 创建下一级待审批流程
                                    $admin_name ='';
                                    if ($flow_next->approve_admin_id){
                                        foreach ($flow_next->approve_admin_id as $apadid) {
                                            $admins = WaAdmins::find(['id' => $apadid]);
                                            $admins = json_decode($admins);
                                            if ($admins) {
                                                $admin_name = $admin_name .'/'. $admins->nickname;
                                            }
                                        }

                                    }

                                    // 添加请款审批详情记录
                                    $approve_log_data = [
                                        'pr_no' => $approves->pr_no, // 请款编号
                                        'check_id' => $approves->id, // 审批id
                                        'created_at' => dateTime(),
                                        'admin_id' => $adminId->id,
                                        'admin_ids' => json_encode($flow_next->approve_admin_id), //
                                        'admin_name' => $admin_name,
                                        'status' => 0, //0 待审批 1审批通过 2 审批驳回
                                    ];
                                    if ($approves->purchase_payroll_sn) {
                                        $approve_log_data['purchase_payroll_sn'] = $approves->purchase_payroll_sn;// 请款编号
                                    }

                                    $up_payroll_flow_status = WaPayrollRequisitionExamineApproveFlow::create($approve_log_data); // 添加销售合同审批详情记录
                                    if (!$up_payroll_flow_status){
                                        return IndexController::errored('','审批流日志创建失败');
                                    }
                                }

                            }

                        }else{
                            return IndexController::errored('','不满足操作条件,请查看合同审批状态 和 审批权限');
                        }
//                    }
                    }
                }

            }elseif ($param['status'] == 2){


                // 审批驳回 修改：请款状态为 审批驳回 添加审批驳回的理由两边都要 添加请款审批详情记录
                // 审批驳回驳回后是创建新的审批流  旧的失败记录保留
                // 手动创建的请款
                // 采购请款单状态 0 草稿(撤回审批)  1 审批中(提交审批) 2 待付款 3 已付款 4 审核不通过 5 删除
                $up_purchase_payroll_status = WaPurchasePayrollRequisition::updated(['status' => 4, 'updated_at' =>dateTime()],['purchase_payroll_sn' => $approves->purchase_payroll_sn ]);// 修采购请款单状态
                if (!$up_purchase_payroll_status){
                    return IndexController::errored('','采购请款单状态修改失败');
                }

                // 修改请款审批单状态 状态 0 审批中 1 通过 2 驳回 3撤回
                $up_purchase_approve_status =WaPayrollRequisitionExamineApprove::updated(['id' => $param['id'] ],['status' => 2,'refuse_reason' => $param['refuse_reason'], 'updated_at' =>dateTime()]);
                if (!$up_purchase_approve_status){
                    return IndexController::errored('','采购请款单审批状态修改失败');
                }
                // 添加请款审批详情记录
                $approve_log_data = [
                    'pr_no' => $approves->pr_no, // 请款编号
                    'check_id' => $approves->id, // 审批id
                    'created_at' => dateTime(),
                    'admin_id' => $adminId->id,
                    'admin_ids' => json_encode($flow->approve_admin_id), //
                    'admin_name' => $adminId->nickname,
                    'refuse_reason' => $refuse_reason,
                    'status' => 2, //0 待审批 1审批通过 2 审批驳回 3作废 4  删除
                ];
                if ($approves->purchase_payroll_sn) {
                    $approve_log_data['purchase_payroll_sn'] = $approves->purchase_payroll_sn;// 请款编号
                }
                $up_purchase_flow_status = WaPayrollRequisitionExamineApproveFlow::create($approve_log_data); // 添加请款审批详情记录
                if (!$up_purchase_flow_status){
                    return  IndexController::errored('','添加审批流日志失败');
                }
            }elseif ($param['status'] == 3){
                // 撤回 -> 整个合同和审批都撤回 并添加操作记录
                return IndexController::errored('','撤回是请款页面操作');
            }else {
                return IndexController::errored('', '请款审批状态没有此状态');
            }
            \support\Db::commit();
            return IndexController::successed('', '采购请款审批状态修改成功');
        } catch (\Throwable $exception) {
            IndexController::logError('payrollRequisitionApproveUpdate', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '采购请款审批状态修改失败');
        }

    }

    /**
     * 请款审批-详情
     * @param Request $request
     * @return \support\Response
     */
    public  function payrollRequisitionApproveDetail(Request $request){

//        process($request);
        $param = $request->all();
// 获取管理员信息
        $adminId = json_decode(adminId($request));
//        $payroll_approve = WaPayrollRequisitionExamineApprove::find($adminId,['purchase_payroll_sn' => $param['purchase_payroll_sn'],
//            ['pr_no','=', $param['pr_no']]]);
        xdebug_var_dump($param['purchase_payroll_sn']);
        $payroll_approve = WaPayrollRequisitionExamineApprove::find($adminId,['purchase_payroll_sn' => $param['purchase_payroll_sn']]);
        $payroll_approve = json_decode($payroll_approve);
        // 获取审批数据 审批
        if ($payroll_approve){
            $payroll_approve->approve_flow = json_decode($payroll_approve->approve_flow);

            foreach ($payroll_approve->approve_flow as $kapprove) {
                $admin_name ='';
                if ($kapprove->approve_admin_id){
                    foreach ($kapprove->approve_admin_id as $apadid) {
                        $admins = WaAdmins::find(['id' => $apadid]);
                        $admins = json_decode($admins);
                        if ($admins) {
                            $department = WaDepartment::find(['id' => $admins->department_id]);
                            $department = json_decode($department);
                            if ($department){
                                $admin_name = $admin_name .'/'. $admins->nickname .'('. $department->name .')';

                            }else{
                                $admin_name = $admin_name .'/'. $admins->nickname;

                            }
                        }
                    }

                }


                $kapprove->check_time = dateTime();
                $kapprove->refuse_reason = '';
                $kapprove->status = 0;
                $kapprove->status_name = '待审批';
                $kapprove->admin_name = $admin_name;
            }
            if ($payroll_approve->initiator_admin_id){
                $admins = WaAdmins::find(['id' => $payroll_approve->initiator_admin_id]);
                $admins = json_decode($admins);
                if ($admins) {
                    $department = WaDepartment::find(['id' => $admins->department_id]);
                    $department = json_decode($department);
                    if ($department){
                        $payroll_approve->initiator_admin_name = $payroll_approve->initiator_admin_name .'('. $department->name .')';
                    }
                }
            }
            // 请款状态  0 草稿 1审批中 2待供应商签署 3已完成 4已作废 5 审核不通过 6 删除
            if ($payroll_approve->status == 0){
                $payroll_approve->status_name = '草稿';
            }elseif ($payroll_approve->status == 1){
                $payroll_approve->status_name = '审批中';
            }elseif ($payroll_approve->status == 2){
                $payroll_approve->status_name = '待供应商签署';
            }elseif ($payroll_approve->status == 3){
                $payroll_approve->status_name = '已完成';
            }elseif ($payroll_approve->status == 4) {
                $payroll_approve->status_name = '已撤回';
            }elseif ($payroll_approve->status == 5) {
                $payroll_approve->status_name = '审核不通过';
            }elseif ($payroll_approve->status == 6) {
                $payroll_approve->status_name = '删除';
            }else{
                $payroll_approve->status_name = '';
            }
            // 审批进度
            if ($payroll_approve->id){
//                $purchase_approve_flow = WaPayrollRequisitionExamineApproveFlow::all(['check_id' => $payroll_approve->id]);
                $purchase_approve_flow = WaPayrollRequisitionExamineApproveFlow::all(['pr_no' => $payroll_approve->pr_no]);
                $purchase_approve_flow = json_decode($purchase_approve_flow);
                if ($purchase_approve_flow){
                    foreach ($purchase_approve_flow as $approve){
                        // 状态
                        if ($approve->status == 0){
                            $approve->status_name = '审批中';
                        }elseif ($approve->status == 1){
                            $approve->status_name = '审批通过';
                            if ($payroll_approve->approve_flow) {
                                foreach ($payroll_approve->approve_flow as $kapprove) {
                                    if (in_array($approve->admin_id,$kapprove->approve_admin_id) && $approve->status == 1 ){
                                        $kapprove->admin_name = $approve->admin_name;
                                        $kapprove->check_time = $approve->updated_at;
                                        $kapprove->refuse_reason = $approve->refuse_reason;
                                        $kapprove->status = $approve->status;
                                        $kapprove->status_name = $approve->status_name;

                                    }
                                }
                            }
                        }elseif ($approve->status == 2){
                            $approve->status_name = '审批驳回';
                            if ($payroll_approve->approve_flow) {
                                foreach ($payroll_approve->approve_flow as $kapprove) {
                                    if (in_array($approve->admin_id,$kapprove->approve_admin_id) && $approve->status == 2 ){
                                        $kapprove->admin_name = $approve->admin_name;
                                        $kapprove->check_time = $approve->created_at;
                                        $kapprove->refuse_reason = $approve->refuse_reason;
                                        $kapprove->status = $approve->status;
                                        $kapprove->status_name = $approve->status_name;

                                    }
                                }
                            }
                        }elseif ($approve->status == 3){
                            $approve->status_name = '已撤回';
                            if ($payroll_approve->approve_flow) {
                                foreach ($payroll_approve->approve_flow as $kapprove) {
                                    if (in_array($approve->admin_id,$kapprove->approve_admin_id) && $approve->status == 3 ){
                                        $kapprove->admin_name = $approve->admin_name;
                                        $kapprove->check_time = $approve->created_at;
                                        $kapprove->refuse_reason = $approve->refuse_reason;
                                        $kapprove->status = $approve->status;
                                        $kapprove->status_name = $approve->status_name;

                                    }
                                }
                            }
                        }else{
                            $approve->status_name = '';
                        }



                    }

                    $admins_by = WaAdmins::find(['id' => $purchase_approve_flow[0]->admin_id]);
                    $admins_by = json_decode($admins_by);
                    if (!$admins_by) {
                        return IndexController::errored('', '获取部门人员失败');
                    }

                    $de = [
                        'admin_id' => $purchase_approve_flow[0]->admin_id,
                        'admin_name' => $purchase_approve_flow[0]->admin_name,
                    ];
                    $payroll_approve->initiator = $de;
                }
                $payroll_approve->purchase_approve_flow = $purchase_approve_flow;
            }else{
                $payroll_approve->purchase_approve_flow = [];
                $payroll_approve->initiator = '';
            }

        }else{
            return IndexController::errored('','暂无请款记录');
        }

        return IndexController::successed(json_encode($payroll_approve),'');
    }

}